import React from "react"
import ReactDOM from "react-dom"
import PasteComponent from "./PasteComponent"
import CopyComponent from "./CopyComponent"
import { Streamlit } from "streamlit-component-lib"

// 从当前 URL 路径获取组件名称
const getComponentName = () => {
  // 获取完整路径
  const fullPath = window.location.pathname
  // console.log("Full path:", fullPath)
  
  // 从路径中提取组件名称
  if (fullPath.includes('copy_component')) {
    return 'copy_component'
  } else if (fullPath.includes('paste_component')) {
    return 'paste_component'
  }
  
  // 如果上面的方法失败，尝试从 title 获取
  const iframeTitle = (window.frameElement as HTMLIFrameElement)?.title  
  if (iframeTitle?.includes('copy_component')) {
    return 'copy_component'
  } else if (iframeTitle?.includes('paste_component')) {
    return 'paste_component'
  }
}

const componentName = getComponentName()
// console.log("Detected component name:", componentName)

// 根据组件名称渲染对应的组件
const renderComponent = () => {
  if (componentName === 'paste_component') {
    return <PasteComponent />
  } else if (componentName === 'copy_component') {
    return <CopyComponent />
  }
}

// 确保 Streamlit 知道组件已经准备好
Streamlit.setComponentReady()

ReactDOM.render(
  <React.StrictMode>
    {renderComponent()}
  </React.StrictMode>,
  document.getElementById("root")
)
